﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace ERPStore.FullTextCatalog.Tests
{
	public class BootStrapper
	{
		public static Microsoft.Practices.Unity.IUnityContainer InitializeContainer()
		{
			var container = new Microsoft.Practices.Unity.UnityContainer();

			container.RegisterType<Serialcoder.Erp.Logging.ILogger, ConsoleLogger>(new ContainerControlledLifetimeManager());
			container.RegisterType<ERPStore.Logging.ILogger, ConsoleLogger>(new ContainerControlledLifetimeManager());

			container.RegisterType<ERPStore.Repositories.ICartRepository, ERPStore.Repositories.HttpContextCartRepository>();
			container.RegisterType<ERPStore.Services.ICacheService, ERPStore.Services.SimpleCacheService>();
			container.RegisterType<ERPStore.Services.ICartService, ERPStore.Services.CartService>();
			container.RegisterType<ERPStore.Services.IEmailerService, ERPStore.Services.EmailerService>();
			container.RegisterType<ERPStore.Services.IScheduledTaskService, ERPStore.Services.ScheduledTaskService>();
			container.RegisterType<ERPStore.Services.IEventSubscriptionService, ERPStore.Services.EventSubscriptionService>(new ContainerControlledLifetimeManager());
			container.RegisterType<ERPStore.Services.IEventPublisher, ERPStore.Services.EventPublisher>(new ContainerControlledLifetimeManager());
			container.RegisterType<ERPStore.Services.ILocalizationService, ERPStore.Services.VoidLocalizationService>(new ContainerControlledLifetimeManager());

			var map = new ExeConfigurationFileMap();
			var unityConfigFileName = ERPStore.Configuration.ConfigurationSettings.AppSettings["unityConfigFileName"] ?? "unity.config";
			map.ExeConfigFilename = @".\" + unityConfigFileName;
			var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
			var section = (UnityConfigurationSection)config.GetSection("unity");
			if (section == null)
			{
				throw new Exception(string.Format("unity section in {0} does not exists", unityConfigFileName));
			}
			section.Configure(container);

			var cryptoProvider = new System.Security.Cryptography.RC2CryptoServiceProvider();
			cryptoProvider.GenerateKey();
			cryptoProvider.GenerateIV();

			var injectionMembers = new InjectionMember[] 
					{ 
						new InjectionConstructor(cryptoProvider.Key, cryptoProvider.IV) ,
					};

			container.RegisterType<ERPStore.Services.CryptoService>(new ContainerControlledLifetimeManager(), injectionMembers);

			var settingsService = container.Resolve<ERPStore.Services.ISettingsService>();
			var webSiteSettings = settingsService.GetWebSiteSettings("http://test");

			new ERPStore.ERPStoreApplication(container);

			var connector = new Serialcoder.Erp.Web.ERP360Connector.PluginInit(container, null, webSiteSettings);
			connector.AddServices();
			connector.Load();

			var dbCatalog = container.Resolve<Serialcoder.Erp.Web.ERP360Connector.CatalogService>();
			var logger = container.Resolve<ERPStore.Logging.ILogger>();
			var ftfolder = ERPStore.Configuration.ConfigurationSettings.AppSettings["erpStoreTempPath"];
			var catalogInjectionMembers = new InjectionMember[] 
					{ 
						new InjectionConstructor(ftfolder, dbCatalog, logger) 
					};

			container.RegisterType<ERPStore.Services.ICatalogService, ERPStore.FullTextCatalog.Services.FullTextCatalogService>(catalogInjectionMembers);

			return container;
		}
	}
}
